package com.sino.operation.advice;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sino.operation.Exception.sinoexception.*;
import com.sino.operation.commons.commonEnum.ClassNameEnum;
import com.sino.operation.commons.commonEnum.StatusInfoEnum;
import com.sino.operation.commons.response.CenterResponseEnum;
import com.sino.operation.entity.BizCityServiceCode;
import com.sino.operation.entity.SysCenter;
import com.sino.operation.entity.SysOrgCenter;
import com.sino.operation.entity.SysTransCenter;
import com.sino.operation.mapper.BizCityServiceCodeMapper;
import com.sino.operation.mapper.SysCenterMapper;
import com.sino.operation.mapper.SysOrgCenterMapper;
import com.sino.operation.mapper.SysTransCenterMapper;
import com.sino.operation.param.out.EscReceiveByCounterToAccount;
import com.sino.operation.param.out.EscReciveByCounter;
import com.sino.operation.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * TOD
 *
 * @author Administrator
 * date 2021/9/23 0023 22:40
 */
@Aspect
@Component
@Slf4j
public class StartJudgmentAdvice {


    @Autowired(required = false)
    private SysOrgCenterMapper sysOrgCenterMapper;

    @Autowired(required = false)
    private SysTransCenterMapper sysTransCenterMapper;

    @Autowired(required = false)
    private BizCityServiceCodeMapper BizCityServiceCodeMapper;

    @Autowired(required = false)
    private SysCenterMapper sysCenterMapper;

    @Autowired(required = false)
    private StringRedisTemplate redisTemplate;

    @Around("@annotation(com.sino.operation.annotation.StartJudgment)")
    public Object judgmentStart(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("进入发起流程自动校验aop中");
        Class<?> aClass = joinPoint.getArgs()[0].getClass();
        PublicInfo publicInfo=null;
        String className = aClass.getName();
        className=className.substring(className.lastIndexOf(".")+1);
        //region 判断借来的参数是哪一种交易的参数类
        if (ClassNameEnum.CURRENT_CLASS_NAME.getParamName().equals(className)){
            EscReceiveByCounterToAccount escReceiveByCounterToAccount=(EscReceiveByCounterToAccount)joinPoint.getArgs()[0];
            publicInfo=escReceiveByCounterToAccount.getEscBodyByAccount().getPublicInfo();
        }else {
            EscReciveByCounter escRequestParam=(EscReciveByCounter)joinPoint.getArgs()[0];
            publicInfo=escRequestParam.getEscBody().getPublicInfo();
        }
        //endregion

        //region 判断公共信息参数是否存在-->渠道来源是否存在
        String orgCode = publicInfo.getOrgCode();
        String channelId = publicInfo.getChannelId();
        //endregion

        //region 根据公共信息的交易码以及机构去查询到作业中心是否存在
        String tranCode = publicInfo.getTranCode();
        String centerNo=null;
        HashOperations<String, Object, Object> hashOperations = redisTemplate.opsForHash();
        Object o = hashOperations.get("sys_trans_center", tranCode);
        if (ObjectUtil.isNotNull(o)){
            centerNo=(String)o;
        }else {
            QueryWrapper<SysTransCenter> sysTransCenterQueryWrapper = new QueryWrapper<>();
            sysTransCenterQueryWrapper.eq("trans_type",tranCode);
            SysTransCenter sysTransCenter = sysTransCenterMapper.selectOne(sysTransCenterQueryWrapper);
            if (sysTransCenter==null){
                //通过机构中心
                Object orgCenter = hashOperations.get("sys_org_center", orgCode);
                if (ObjectUtil.isNotNull(orgCenter)){
                    centerNo=(String)orgCenter;
                }else {
                    QueryWrapper<SysOrgCenter> sysOrgCenterQueryWrapper = new QueryWrapper<>();
                    sysOrgCenterQueryWrapper.eq("org_id",orgCode);
                    SysOrgCenter sysOrgCenter = sysOrgCenterMapper.selectOne(sysOrgCenterQueryWrapper);
                    if (sysOrgCenter==null){
                        throw new CenterNoOrgException(CenterResponseEnum.CENTER_NO_ORG.getCode(),CenterResponseEnum.CENTER_NO_ORG.getMessage());
                    }else {
                        centerNo=sysOrgCenter.getCenterNo();
                        hashOperations.put("sys_org_center",orgCode,centerNo);
                    }
                }

            }else {
                centerNo=sysTransCenter.getCenterNo();
                hashOperations.put("sys_trans_center",tranCode,centerNo);
            }
            if (StringUtils.isBlank(centerNo)||StringUtils.isEmpty(centerNo)){
                throw new CenterNullException(CenterResponseEnum.CENTER_NULL.getCode(),CenterResponseEnum.CENTER_NULL.getMessage());
            }else{
                publicInfo.setCenterNo(centerNo);
            }
        }

        //endregion

        //region 根据渠道来源信息去查询该渠道来源是否需要作业中心是否开启
        QueryWrapper<BizCityServiceCode> serviceCodeQueryWrapper = new QueryWrapper<>();
        serviceCodeQueryWrapper.eq("channel_code",channelId);
        BizCityServiceCode bizCityServiceCode = BizCityServiceCodeMapper.selectOne(serviceCodeQueryWrapper);
        if (bizCityServiceCode!=null){
            if (StatusInfoEnum.CENTER_FLAG_IS.equals(bizCityServiceCode.getCenterFlag())){
                QueryWrapper<SysCenter> sysCenterQueryWrapper = new QueryWrapper<>();
                sysCenterQueryWrapper.eq("center_no", centerNo);
                SysCenter sysCenter = sysCenterMapper.selectOne(sysCenterQueryWrapper);
                if (sysCenter!=null){
                    if (StatusInfoEnum.CENTER_STATE_OFF.equals(sysCenter.getCenterState())){
                        throw new CenterStateException(CenterResponseEnum.CENTER_STATE_OFF.getCode(),CenterResponseEnum.CENTER_STATE_OFF.getMessage());
                    }
                }else {
                    throw new CenterStateNoConfigException(CenterResponseEnum.CENTER_STATE_NO_CONFIG.getCode(),CenterResponseEnum.CENTER_STATE_NO_CONFIG.getMessage());
                }
            }
        }else {
            throw new SysServiceCodeNoConfigException(CenterResponseEnum.CHANNEL_NO_CONFIG.getCode(),CenterResponseEnum.CHANNEL_NO_CONFIG.getMessage());
        }
        //endregion
        return joinPoint.proceed();
    }
}
